package com.ruoyi.project.user.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.RandomUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.price.domain.BinsAccount;
import com.ruoyi.project.price.service.IBinsAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.user.mapper.UserInfoMapper;
import com.ruoyi.project.user.domain.UserInfo;
import com.ruoyi.project.user.service.IUserInfoService;

/**
 * 用户信息Service业务层处理
 * 
 * @author administrator
 * @date 2025-04-03
 */
@Service
public class UserInfoServiceImpl implements IUserInfoService 
{
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    Snowflake snowflake;

    /**
     * 查询用户信息
     * 
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public UserInfo selectUserInfoByUserId(String userId)
    {
        return userInfoMapper.selectUserInfoByUserId(userId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param userInfo 用户信息
     * @return 用户信息
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo)
    {
        return userInfoMapper.selectUserInfoList(userInfo);
    }

    /**
     * 新增用户信息
     * 
     * @param userInfo 用户信息
     * @return 结果
     */
    @Override
    public int insertUserInfo(UserInfo userInfo)
    {

        userInfo.setUserId(userInfo.getDriverContact());
        userInfo.setCreateTime(DateUtils.getNowDate());
        UserInfo userInfo_data = userInfoMapper.selectUserInfoByUserId(userInfo.getUserId());
        if (userInfo_data!=null){
            return userInfoMapper.updateUserInfo(userInfo);
        }
        return userInfoMapper.insertUserInfo(userInfo);
    }

    @Autowired
    private IBinsAccountService binsAccountService;
    /**
     * 修改用户信息
     * 
     * @param userInfo 用户信息
     * @return 结果
     */
    @Override
    public int updateUserInfo(UserInfo userInfo)
    {
        userInfo.setUpdateTime(DateUtils.getNowDate());
        int count =  userInfoMapper.updateUserInfo(userInfo);
        if (count==1){
            if ("0".equals(userInfo.getDriverStatus())){
                BinsAccount binsAccount = new BinsAccount();
                binsAccount.setUserId(userInfo.getUserId());
                binsAccount.setTotalAmount(BigDecimal.ZERO);
                binsAccount.setAccountNum(RandomUtil.randomString(16));
                binsAccountService.insertBinsAccount(binsAccount);
            }
        }
        return count;
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserInfoByUserIds(Long[] userIds)
    {
        return userInfoMapper.deleteUserInfoByUserIds(userIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserInfoByUserId(String userId)
    {
        return userInfoMapper.deleteUserInfoByUserId(userId);
    }
}
